package com.portalEsporteClubeBahia.app.bean;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.portalEsporteClubeBahia.app.ApplicationException;
import com.portalEsporteClubeBahia.app.bo.UserBO;
import com.portalEsporteClubeBahia.app.vo.UserVO;

@ManagedBean(name="loginBean")
@SessionScoped
public class LoginBean implements Serializable{

	private static final long serialVersionUID = 807418769699003584L;
	private UserVO user;
	private String message;
	
	public LoginBean(){
		user = new UserVO();
		message = "";
	}
	
	public String login(){
		
		try{
			user = new UserBO().findByUserPassword(user);	  
		}catch(ApplicationException e){
			if(e.getCause()!=null)
				message = e.getCause().getMessage();
			else
				message = e.getMessage();
			return "login.jsf";
		}
		
		return "main.jsf";
	}
	
	public String quit(){
		HttpSession ses = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);   
        ses.removeAttribute("user");
        return "login.jsf";
	}

	public UserVO getUser() {
		return user;
	}

	public void setUser(UserVO user) {
		this.user = user;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
		
}
